package com.xiaozhen.quartz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.quartz.config.JobAndTrigger;
import com.xiaozhen.quartz.param.QuartzParam;
import com.xiaozhen.quartz.service.JobService;
import com.xiaozhen.security.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author zhenweisheng
 * @date 2020/9/30 9:50
 * @description:
 */
@Api(tags = "定时任务管理接口")
@RestController
@RequestMapping("job")
public class JobController extends BaseController {

    @Autowired
    private JobService jobService;

    @ApiOperation("查询定时任务列表")
    @GetMapping("/queryJob")
    public Map<String, Object> queryJob(int pageNum, int pageSize) {
        Page<JobAndTrigger> page = jobService.queryJob(pageNum, pageSize);
        return getDataTable(page);
    }

    @ApiOperation("添加定时任务")
    @PostMapping("/addJob")
    public void addJob(@RequestBody QuartzParam param) throws Exception {
        jobService.addJob(param.getJobClassName(), param.getJobGroupName(), param.getCronExpression(), param.getJobName());
    }

    @ApiOperation("删除定时任务")
    @PostMapping("/deleteJob")
    public void deleteJob(@RequestBody QuartzParam param) throws Exception {
        jobService.jobDelete(param.getJobName(), param.getJobGroupName());
    }

    @ApiOperation("暂停定时任务")
    @PostMapping("/pauseJob")
    public void pauseJob(@RequestBody QuartzParam param) throws Exception {
        jobService.jobPause(param.getJobName(), param.getJobGroupName());
    }

    @ApiOperation("恢复定时任务")
    @PostMapping("/resumeJob")
    public void resumeJob(@RequestBody QuartzParam param) throws Exception {
        jobService.jobResume(param.getJobName(), param.getJobGroupName());
    }

    @ApiOperation("修改定时任务")
    @PostMapping("/rescheduleJob")
    public void rescheduleJob(@RequestBody QuartzParam param) throws Exception {
        jobService.jobReschedule(param.getJobName(), param.getJobGroupName(), param.getCronExpression());
    }
}

